<template>
  <el-card>
    <el-table border style="margin: 10px 0px" :data="skuArr">
      <el-table-column label="序号" type="index" align="center" width="100px"></el-table-column>
      <el-table-column label="名称" show-overflow-tooltip width="200px" prop="skuName"></el-table-column>
      <el-table-column label="描述" show-overflow-tooltip width="200px" prop="skuDesc"></el-table-column>
      <el-table-column label="图片" width="200px">
        <template #="{ row }">
          <img :src="row.skuDefaultImg" alt="" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column label="重量" width="200px" prop="weight"></el-table-column>
      <el-table-column label="价格" width="200px" prop="price"></el-table-column>
      <el-table-column label="操作" width="380px" fixed="right">
        <template #="{ row }">
          <el-button type="primary" size="small" :icon="row.isSale == 1 ? 'Bottom' : 'Top'" @click="updateSale(row)"></el-button>
          <el-button type="primary" size="small" icon="Edit" @click="updateSku"></el-button>
          <el-button type="primary" size="small" icon="InfoFilled" @click="findSku(row)"></el-button>
          <el-popconfirm width="200px" :title="`确定删除${row.skuName}?`" @confirm="removeSku(row)">
            <template #reference>
              <el-button type="primary" size="small" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[10, 15, 20, 25]" :background="true" layout="prev, pager, next, jumper,->,sizes,total" :total="total" @current-change="getHasSku" @size-change="handler" />
    <!-- 抽屉组件:展示商品详情 -->
    <el-drawer v-model="drawer">
      <!-- 标题部分 -->
      <template #header>
        <h4>查看商品的详情</h4>
      </template>
      <template #default>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag style="margin: 5px" v-for="item in skuInfo.skuAttrValueList" :key="item.id">{{ item.attrName }}</el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag style="margin: 5px" v-for="item in skuInfo.skuSaleAttrValueList" :key="item.id">{{ item.saleAttrName }}</el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                <img :src="item.imgUrl" alt="" style="width: 100%; height: 100%" />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import { reqSkuList, reqSaleSku, reqCancelSale, reqSkuInfo, reqRemoveSku } from '@/api/product/sku/index.ts'
//引入ts类型
import type { SkuData } from '@/api/product/sku/type'
import { ElMessage } from 'element-plus'
import { ref, onMounted } from 'vue'
//分页器当前页码
let pageNo = ref<number>(1)
//每一页展示几条数据
let pageSize = ref<number>(10)
let total = ref<number>(0)
let skuArr = ref<SkuData[]>([])
//抽屉默认不展示
let drawer = ref<any>(false)
let skuInfo = ref<any>({})
//组件挂载完毕
onMounted(() => {
  getHasSku()
})

let getHasSku = async (pager = 1) => {
  //当前分页器的页码
  pageNo.value = pager
  let res = await reqSkuList(pageNo.value, pageSize.value)
  if (res.code == 200) {
    total.value = res.data.total
    skuArr.value = res.data.records
  }
}
//分页器每页展示条数的回调
let handler = () => {
  getHasSku()
}
//点击上下架的回调
let updateSale = async (row: SkuData) => {
  //如果是1说明是上架状态，点击按钮应该是下架
  if (row.isSale == 1) {
    //下架
    await reqCancelSale(row.id as number)
    ElMessage({ type: 'success', message: '下架成功' })
    //请求数据
    getHasSku(pageNo.value)
  } else {
    //上架
    await reqSaleSku(row.id as number)
    ElMessage({ type: 'success', message: '上架成功' })
    //请求数据
    getHasSku(pageNo.value)
  }
}
//点击修改的按钮
let updateSku = () => {
  ElMessage({ type: 'success', message: '正在开发中...' })
}
//点击商品详情的回调
let findSku = async (row: SkuData) => {
  let res = await reqSkuInfo(row.id as number)
  if (res.code == 200) {
    skuInfo.value = res.data
  }
  //打开抽屉
  drawer.value = true
}
//删除商品
let removeSku = async (row: SkuData) => {
  let res = await reqRemoveSku(row.id as number)
  if (res.code == 200) {
    ElMessage({ type: 'success', message: '删除成功' })
    //请求数据
    getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({ type: 'error', message: '系统数据不能删除' })
  }
}
</script>

<style scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
